<?php

namespace App\Controllers\Admin;
use Sentry,User,DB,Area,Mount,Pointrecord,Mountrecord;
use App;
use BaseController,View,Input,Form,Order,Customer,Redirect;
class CustomersController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 * GET /admin/customers
	 *
	 * @return Response
	 */
	public function index()
	{
		//
        $user = Sentry::getUser();


        if ($user->hasAccess('admin')){
            if(Input::get('type') > 0){
                $customers = Customer::where('type',Input::get('type'))->orderBy('created_at', 'DESC')->paginate(20);
            }elseif(Input::get('type') == 'more'){
                $customers = Customer::where('mount_count','>',1)->orderBy('created_at', 'DESC')->paginate(20);
            }else{
                $customers = Customer::orderBy('created_at', 'DESC')->paginate(20);
            }
        }elseif($user->hasAccess('city')){
            if(Input::get('type') > 0){
                $customers = Customer::where('city',$user->city)->where('type',Input::get('type'))->orderBy('created_at', 'DESC')->paginate(20);
            }elseif(Input::get('type') == 'more'){
                $customers = Customer::where('city',$user->city)->where('mount_count','>',1)->orderBy('created_at', 'DESC')->paginate(20);
            }else{
                $customers = Customer::where('city',$user->city)->orderBy('created_at', 'DESC')->paginate(20);

            }
        }elseif($user->hasAccess('section')){
            if(Input::get('type') > 0){
                $customers = Customer::where('area',$user->area)->where('type',Input::get('type'))->orderBy('created_at', 'DESC')->paginate(20);
            }elseif(Input::get('type') == 'more'){
                $customers = Customer::where('area',$user->area)->where('mount_count','>',1)->orderBy('created_at', 'DESC')->paginate(20);
            }else{
                $customers = Customer::where('area',$user->area)->orderBy('created_at', 'DESC')->paginate(20);

            }
        }
        $type = Input::get('type');
        $METHOD = 'customers';
        $ACTION = 'index';
        return \View::make('admin.customers.index',compact('METHOD','ACTION','customers','user','type'));
	}

    public function change(){
        $id = Input::get('id');
        $type = Input::get('type');
        $customer = Customer::find($id);
        $customer->type = $type;
        $customer->save();
        echo 1;
    }
    public function mount_info(){
        $id = Input::get('id');
        $mounts = Mount::where('customer_id',$id)->get();
        return View::make('admin.customers.mount_info',compact('mounts'));
    }
    public function points(){
        $id = Input::get('id');
        $pointrecords = Pointrecord::where('customer_id',$id)->orderBy('created_at', 'DESC')->paginate(20);
        return View::make('admin.customers.points',compact('pointrecords','id'));
    }

    public function affiliates(){
        $id = Input::get('id');
        $mountrecords = Mountrecord::where('invite_id',$id)->orderBy('created_at', 'DESC')->paginate(20);
        return View::make('admin.customers.affiliates',compact('mountrecords','id'));
    }
    public function points_edit(){
        $user = Sentry::getUser();
        $count = (int)Input::get('ccount');
        $action = Input::get('action');
        $add = Input::get('add');
        $id = Input::get('customer_id');
        $customer = Customer::find($id);
        if($add == 0 && $customer->all_points-$customer->use_points-$count < 0){
            dd('积分不够消费');
        }else{
            if($add == 0){
                $customer->use_points = $customer->use_points+$count;
            }elseif($add == 1){
                $customer->all_points = $customer->all_points+$count;
            }

            $customer->save();
            Pointrecord::new_record($count,$action,$add,0,$id,$user->id);
            return Redirect::to($_SERVER["HTTP_REFERER"]);
        }
    }

    public function exchange(){

        if($_POST){
            if(Input::get('city') && Input::get('zhi')){
                $customer = Customer::find(Input::get('customer_id'));
                $customer->city = Input::get('city');
                $zhi = User::find(Input::get('zhi'));
                $customer->area = $zhi->area;
                $customer->save();

                $mounts = Mount::where('customer_id',$customer->id)->get();
                foreach($mounts as $mount){
                    $mount = Mount::find($mount->id);
                    $mount->city = Input::get('city');
                    $mount->section_id = $zhi->area;
                    $mount->area_id = Input::get('xiaoqu');
                    $mount->save();
                }

            }
            echo "<script>alert('转移成功！')</script>";
        }

        $user = Sentry::getUser();

        // Check if the user has the 'admin' permission. Also,
        // multiple permissions may be used by passing an array
//        if ($user->hasAccess('admin')){
        $myusers = User::where('permissions','{"city":1}')->get();
//        }
//
//        $METHOD = 'cities';
//        $ACTION = 'index';


        return View::make('admin.customers.exchange',compact('myusers'));
    }


    public function ajax_exchange(){
        $v = Input::get('v');
        $vs = User::where('permissions','{"section":1}')->where('city',$v)->get();
        $html = '';
        foreach($vs as $v){
            $html = $html."<option value='".$v->id."'>".$v->username."-".$v->mobile."</option>";
        }
        echo $html;
    }

    public function ajax_xiaoqu_exchange(){

        $v = Input::get('v');
        $vs = User::where('permissions','{"section":1}')->where('city',$v)->first();
        $areas = Area::where('father_id',$vs->area)->orderBy('created_at', 'DESC')->get();
        $html = '';
        foreach($areas as $v){
            $html = $html."<option value='".$v->id."'>".$v->name."</option>";
        }
        echo $html;
    }
    public function ajax_xiaoqu_exchange2(){

        $v = Input::get('v');
        $vs = User::find($v);
        $areas = Area::where('father_id',$vs->area)->orderBy('created_at', 'DESC')->get();
        $html = '';
        foreach($areas as $v){
            $html = $html."<option value='".$v->id."'>".$v->name."</option>";
        }
        echo $html;
    }
	/**
	 * Show the form for creating a new resource.
	 * GET /admin/customers/create
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 * POST /admin/customers
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 * GET /admin/customers/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 * GET /admin/customers/{id}/edit
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 * PUT /admin/customers/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 * DELETE /admin/customers/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}